package com.wanmait.shoppingsys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.shoppingsys.util.DbHelper;
import com.wanmait.shoppingsys.util.ManagePager;
import com.wanmait.shoppingsys.vo.Goods;
import com.wanmait.shoppingsys.vo.UserInfo;



public class UserinfoDAO {

	private static UserinfoDAO userinfoDAO;
	
	private UserinfoDAO() {}
	
	public static UserinfoDAO getDAO() {
		if(userinfoDAO==null) {
			userinfoDAO = new UserinfoDAO();
			
		}
		return userinfoDAO;
	}
	
	public void updatePass(Integer id,String pass) { //修改密码
		String sql = "UPDATE userinfo SET pass=? WHERE id=? ";
		ArrayList params = new ArrayList();
		params.add(pass);
		params.add(id);
		
		DbHelper.update(sql, params);
	}
	
	public UserInfo findemail(String name,String email)//找回密码
	{
		UserInfo Userinfo = null;
		String sql="SELECT * FROM userinfo WHERE name=? AND email=?";
		Connection conn = DbHelper.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, email);
			rs = ps.executeQuery();
			if(rs.next()) {
				Userinfo = new UserInfo();
				Userinfo.setName(rs.getString("name"));
				Userinfo.setPass(rs.getString("pass"));
				Userinfo.setId(rs.getInt("id"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbHelper.close(rs, ps, conn);
		}
		
		return Userinfo;
	}
	
	public ArrayList<UserInfo> findall() //查找所有
	{
		ArrayList params=new ArrayList();
		String sql="SELECT * FROM userinfo";
		 Connection conn=  DbHelper.getConn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 
		    try {
				ps  = conn.prepareStatement(sql);
				  rs = ps.executeQuery();
				  while(rs.next())
				  {
					  UserInfo userinfo=new UserInfo();
					  params.add(userinfo);
				  }
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		    finally {
		       DbHelper.close(rs, ps, conn);
		    }
		
		
		
		return params;
	}
	
	public String findinfo(String userinfo) 
	{
		String info=null;
		String sql="select userinfo.name from userinfo where userinfo.name=?";
		 Connection conn=  DbHelper.getConn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 
		    try {
				ps  = conn.prepareStatement(sql);
				ps.setString(1, userinfo);
				rs = ps.executeQuery();
				
				  while(rs.next())
				  {
					 
					info = rs.getString("name");
				  }
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		    finally {
		       DbHelper.close(rs, ps, conn);
		    }
		
		
		
		return info;
	}
	
	
	
	public void insert(UserInfo userinfo) {  //注册
		String sql = "INSERT into userinfo(name,pass,email)VALUES(?,?,?)";
		ArrayList params = new ArrayList();
		params.add(userinfo.getName());
		params.add(userinfo.getPass());
		params.add(userinfo.getEmail());
	
		
		DbHelper.update(sql, params);
	}
	
	
	
	public UserInfo findByNameAndPass(String userName,String userPass) {//登录
		UserInfo userinfo = null;
		
		String sql = "SELECT * FROM userinfo WHERE name=? AND pass=?";
		Connection conn = DbHelper.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, userPass);
			rs = ps.executeQuery();
			if(rs.next()) {
				userinfo = new UserInfo();
				
				userinfo.setId(rs.getInt("id"));
				userinfo.setName(rs.getString("name"));
				userinfo.setPass(rs.getString("pass"));
				userinfo.setEnable(rs.getBoolean("enable"));
				userinfo.setEmail(rs.getString("email"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DbHelper.close(rs, ps, conn);
		}
		
		return userinfo;
		
	}
	
	
	public void update(Integer id,Integer en) {//逻辑删除用户
		String sql = "UPDATE userinfo SET enable=? where id=?";
		ArrayList params = new ArrayList();
		params.add(en);
		params.add(id);
		DbHelper.update(sql,params);
	}
	
	
}
